package no.hib.tod141.hasj.servlets;

import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.jstl.core.Config;

/**
 * Servlet implementation class InitSessionServlet
 */
@WebServlet("/InitSession")
public class InitSessionServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public InitSessionServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Cookie[] cookies = request.getCookies();
        if (cookies != null) {
                for (Cookie cookie : cookies) {
                        if (cookie.getName().equals("locale")) {
                                // Set locale from cookie
                                Config.set(request.getSession(), Config.FMT_LOCALE,
                                                cookie.getValue());
                                request.getSession().setAttribute("locale", cookie.getValue());
                        }
                }
        } else {
                //default to english locale
                Config.set(request.getSession(), Config.FMT_LOCALE, "en_GB");
                request.getSession().setAttribute("locale", "en_GB");
        }
       
        RequestDispatcher dispatcher = request.getRequestDispatcher("index.jsp");
        dispatcher.forward(request,response);

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
